package ai.people.platform.archives.controller;


import ai.people.netmon.framework.domain.archives.request.InformationSearchRequest;
import ai.people.netmon.framework.domain.archives.vo.InformationSearchProgressVO;
import ai.people.netmon.framework.domain.archives.vo.InformationSearchRespListVO;
import ai.people.netmon.framework.domain.jx.vo.JxInformationSearchCacheReqVO;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.archives.service.InformationSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 信息查询接口
 * @author LiGuanNan
 * @date 2022/5/31 下午2:23
 */
@RestController
@RequestMapping("v2/information")
public class InformationV2Controller {

    @Autowired
    private InformationSearchService informationSearchService;

    /**
     * 发起信息检索
     *
     * @param reqParam 请求参数
     * @return {@link Result}<{@link InformationSearchRespListVO}<{@link InformationSearchRespListVO}>>
     */
    @PostMapping("/start-search")
    public Result<Long> startSearch(@Validated @RequestBody InformationSearchRequest reqParam) {
        if(StringUtils.isBlank(reqParam.getSearchKeywords())){
            return ResultUtil.success(PageResponse.builderEmpty());
        }
        return ResultUtil.success(informationSearchService.startSearch(reqParam));
    }

    /**
     * 轮询拉取检索结果，建议500ms请求一次
     *
     * @param sid 搜索请求唯一标识
     */
    @GetMapping("/get-search-result/{sid}")
    public Result<InformationSearchProgressVO> getSearchResult(@PathVariable(value = "sid") Long sid) {
        return ResultUtil.success(informationSearchService.getSearchResult(sid));
    }

    /**
     * 设置jx信息搜索缓存
     *
     * @param reqVO 申请签证官
     * @return {@link Result}<{@link Integer}>
     */
    @PostMapping("/set-jx-ins-cache")
    public Result<Integer> setJxInformationSearchCache(@RequestBody JxInformationSearchCacheReqVO reqVO){
        informationSearchService.setJxInformationSearchCache(reqVO.getSearchKeyword(), reqVO.getSearchType(), reqVO.getValue());
        return ResultUtil.success(1);
    }

    /**
     * 信息检索V2版本-列表返回所有字段
     *
     * @param reqParam 请求参数
     * @return {@link Result}<{@link InformationSearchRespListVO}<{@link InformationSearchRespListVO}>>
     */
    @PostMapping("/search")
    public Result<PageResponse<InformationSearchRespListVO>> search(@Validated @RequestBody InformationSearchRequest reqParam) {
        if(StringUtils.isBlank(reqParam.getSearchKeywords())){
            return ResultUtil.success(PageResponse.builderEmpty());
        }
        return ResultUtil.success(informationSearchService.searchV2(reqParam));
    }

}
